package com.sz.biz.app.web.api;

import com.sz.biz.app.dto.LoginDataDto;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.exception.BizException;
import com.sz.common.base.utils.DateUtils;
import io.swagger.annotations.Api;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Function: 获取上下文信息描述 <br>
 * Author: Charles <br>
 * Date: 2016-11-02 10:47:00
 */
@RestController
@RequestMapping("/dev")
@Api(description = " ", tags = "100、Dev")
public class Dev {
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public ResultDto serverlogin(@RequestParam Date date) throws BizException {
        //获取当前的Subject
        ResultDto dto = new ResultDto();
        dto.setData(DateUtils.formatDetailDate(date));
        return dto;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResultDto serverlogin(@RequestBody LoginDataDto loginDataDto) throws BizException {
        UsernamePasswordToken token = new UsernamePasswordToken(loginDataDto.getUserName(), loginDataDto.getPassWord());
        //获取当前的Subject
        ResultDto dto = new ResultDto();
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            dto.setData(subject.getPrincipal());
        } catch (Exception ex) {
            dto.setErrorCode(500);
            dto.setMsg(ex.toString());
        }
        return dto;
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public ResultDto serverlogout() throws BizException {
        SecurityUtils.getSubject().logout();
        ResultDto dto = new ResultDto();
        return dto;
    }
}
